<?php


namespace app\api\controller\v23;


use app\api\controller\v13\Auth2;
use app\common\lib\Util;
use app\common\model\Article as ArticleModel;
use app\common\model\Recode as RecodeModel;

class Article extends Auth2
{
    protected $colors = [
        0 => ['name' => '困难', 'color' => '#7775ff',],
        1 => ['name' => '警戒', 'color' => '#ffa536',],
        2 => ['name' => '平平', 'color' => '#ffed17',],
        3 => ['name' => '吉利', 'color' => '#00f4e7',],
        4 => ['name' => '大吉', 'color' => '#00e9a0',],
    ];

    public function fortune_detail()
    {
        $in_sign = input('in_sign/d', 0);
        //若用户登陆了且有档案则，获取用户的落座
        if (!$in_sign && $this->user && $this->user['rid'])
        {
            $map = ['uid' => $this->user['id'], 'isself' => 1];
            $recode_info = (new RecodeModel())->getSelfRecode($map);

            if ((!$recode_info || !$recode_info['sun']))
            {
                return show(0, '档案缺失请添加档案');
            }
            $in_sign = explode('-', $recode_info['sun'])[0];
        } elseif (!in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
        {
            return show(0, '星座未知');
        }

        $week = (new ArticleModel())->getFortune('week', $in_sign);
        $week['time_range'] = date('Y.m.d', strtotime($week['publish_time'])) . '-' . date('Y.m.d', strtotime($week['publish_time'] . ' +6 day'));
        $month = (new ArticleModel())->getFortune('month', $in_sign);
        $month['time_range'] = date('Y.m.01', strtotime($month['publish_time'])) . '-' . date('m.d', strtotime($month['publish_time'] . ' +1 month -1 day'));
        $year = (new ArticleModel())->getFortune('year', $in_sign);
        $year['time_range'] = date('Y.01.01', strtotime($year['publish_time'])) . '-12.31';

        $fortuneData = [
            'fortune_week' => $week,
            'fortune_month' => $month,
            'fortune_year' => $year
        ];
        $resData = [
            'luck_adversity' => $this->colors,
        ];
        foreach ($fortuneData as $key => $fortuneItem)
        {
            $scoreData = [
                [
                    'name' => '整体',
                    'score' => $fortuneItem['whole'] ?? 0,
                ],
                [
                    'name' => '感情',
                    'score' => $fortuneItem['love'] ?? 0,
                ],
                [
                    'name' => '财富',
                    'score' => $fortuneItem['money'] ?? 0,
                ],
                [
                    'name' => '事业',
                    'score' => $fortuneItem['career'] ?? 0,
                ],
                [
                    'name' => '健康',
                    'score' => $fortuneItem['health'] ?? 0,
                ]
            ];
            $scoreList = [];
            foreach ($scoreData as $datum)
            {
                $resScore = Util::getScorePercentInfo2($datum['score']);
                $resScore['name'] = $datum['name'];
                $scoreList[] = $resScore;
            }


            $resData[$key] = [
                'title' => $fortuneItem['introduction'],
                'introduction' => $fortuneItem['time_range'],
                'content' => $fortuneItem['content'],
                'score_list' => $scoreList,
            ];
        }

        return show(config('code.success'), '请求成功', $resData);
    }
}
